package table;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import table.inter.ICol;
import table.node.BaseNode;
import table.node.DataNode;
import table.tools.NameValues;

public class Col implements ICol{
	/*
	 * base info
	 */
	private String name;
	private String descripes;

	private String dataStruct;
	private String valueType;
	private DataTable BTable;

	/*
	 * flags
	 */
	private String flags;
	private NameValues properties;
	public void setProperties(NameValues properties) {
		this.properties = properties;
	}
	public NameValues getProperties() {
		return properties;
	}
	public boolean hasProperty(String name){
		try{
        if(this.getProperties()!=null && this.getProperties().hasName(name)){
        	return true;
        }} catch(Exception e){}
		return false;
	}
	/*
	 * data 
	 */
	private List<DataNode> dataList;
	
	
	/*
	 * S&Geter
	 */
	
	public void setName(String name) {
		this.name = name;
	}
	public String getName() {
		return name;
	}

	public void setDescripes(String descripes) {
		this.descripes = descripes;
	}
	public String getDescripes() {
		return descripes;
	}
	public void setValueType(String type) {
		this.valueType = type;
	}
	public String getValueType() {
		
		return valueType;
	}

	public void setDataStruct(String dataStruct) {
		this.dataStruct = dataStruct;
	}
	public String getDataStruct() {
		return dataStruct;
	}
	
	/**
	 * @param flags the flags to set
	 */
	public void setFlags(String flags) {
		this.flags = flags;
	}
	/**
	 * @return the flags
	 */
	public String getFlags() {
		return flags;
	}

	
	
	/*
	 * API
	 */
	
	public void addData(DataNode e){
		if(this.dataList == null){
			this.dataList = new ArrayList<DataNode>();
		}
		try {
			this.dataList.add(e);
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}
	public void clearData(){
		if(this.dataList == null){
			this.dataList = new ArrayList<DataNode>();
		}
		try {
			this.dataList.clear();
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}
	
	public DataNode setNode(Object value){
		DataNode node = new BaseNode();
		try {
			node.setNodeValue(this.getColValue(value));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return node;
	}
	
	public DataNode getNode(int i) {
		return this.getDataList().get(i);
	}
	
	public Object getValue(int i){
		try {
			return this.getColValue(this.getNode(i).getNodeValue());
		} catch (Exception e) {
		}
		return null;
	}
	
	public Object getColValue(Object v){
		try {
			if(this.getValueType().toLowerCase().equalsIgnoreCase("int")){
				if(v != null && v.toString().toLowerCase().matches("([1-9][0-9]*)|([0-9])"))	return Integer.parseInt(v.toString());
				return 0;
			} else if(this.getValueType().toLowerCase().equalsIgnoreCase("float")){
				if(v != null && v.toString().toLowerCase().matches("[\\+\\-]?(([1-9][0-9]*[\\.][0-9]*)|([0-9]\\.[0-9]*)|([1-9][0-9]*)|([0-9]))"))	return Float.parseFloat(v.toString());
				return 0;
			} else if(this.getValueType().toLowerCase().equalsIgnoreCase("string")){
				if(v == null || v.toString().equalsIgnoreCase("null")){ return "";}
				return v.toString();
			} else if(this.getValueType().toLowerCase().equalsIgnoreCase("uniqueidentifier")){
				if(v == null || v.toString().length() <= 0){ return "";}
				if(v.toString().startsWith("{")) return v.toString();
				else return "{" + v.toString() + "}";
			}
		} catch (Exception e){
			return v;
		}
		return v;
	}
	
	public int getDataSize(){
		if(this.getDataList() == null) return 0;
		return this.getDataList().size();
	}
	public List<DataNode> getDataList(){
		return this.dataList;
	}
	public DataNode run(HttpServletRequest request){
		return null;
	}
	public boolean insert(){ 
		return false;
	}
	
	public boolean modify(){
		return false;
	}
	
	public boolean delete(){
		return false;
	}
	
	/*
	 * old API
	 */

	public void setBTable(DataTable bTable) {
		BTable = bTable;
	}
	public DataTable getBTable() {
		return BTable;
	}
	public void setDataByCols(List<Col> cols) {
	}
	
	public String toString(){
		return this.getDescripes() + ":" + this.getDataSize() + "["+"]";
	}
}
